package com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.provider

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.request.RequestOptions
import com.example.library_common.glidmodule.GlideApp
import com.example.library_common.utils.QMUIDisplayHelper
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.bean.NewSaleBannerEntity
import com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.bean.NewSaleEntity
import com.youth.banner.adapter.BannerAdapter
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

class NewSaleBannerAdapter(data: MutableList<NewSaleBannerEntity>?, context: Context) :
    BannerAdapter<NewSaleBannerEntity, NewSaleBannerAdapter.BannerViewHolder>(data) {

    private var layoutInflater: LayoutInflater? = null

    private var mContext: Context = context

    init {
        layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    }

    class BannerViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val imageView: ImageView = view.findViewById(R.id.mBannerImageView)

    }

    override fun onCreateHolder(parent: ViewGroup?, viewType: Int): BannerViewHolder {
        val inflate = layoutInflater
            ?.inflate(R.layout.new_sale_banner_layout, parent, false) ?: View(mContext)

        inflate.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        return BannerViewHolder(inflate)
    }

    override fun onBindView(
        holder: BannerViewHolder,
        data: NewSaleBannerEntity?,
        position: Int,
        size: Int
    ) {
        val radius = QMUIDisplayHelper.dp2px(mContext, 6)
        val mRequestOptions = RequestOptions
            .bitmapTransform(
                RoundedCornersTransformation(radius, 0)
            )
        GlideApp.with(mContext)
            .load(data?.modUrl)
            .placeholder(R.drawable.img_erros_bg_rect)
//            .apply(mRequestOptions)
            .centerCrop()
            .into(holder.imageView)
    }

    fun addAll(collection: Collection<NewSaleBannerEntity>?) {
        mDatas.clear()
        mDatas.addAll(collection ?: emptyList<NewSaleBannerEntity>())
        notifyDataSetChanged()
    }
}